package cn.neu.leon.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.batik.apps.rasterizer.SVGConverterException;
import org.apache.commons.codec.binary.Base64;

import cn.neu.leon.util.MyConverter;

public class ExportServlet extends HttpServlet {


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");//注意编码 
		/*
		 * 第一步：获取参数
		 */
		String svg = request.getParameter("svg");

		String width = request.getParameter("width");
		
		String type = request.getParameter("type");
		
		String filename = request.getParameter("filename");
		
		// method，有两种情况，download为将生成的文件下载、save为将png图片保存在服务器端
		String method = request.getParameter("method");
		
		/*
		 * 第二步：生成 .svg文件
		 */
		@SuppressWarnings("deprecation")
		String WebRoot = request.getRealPath("")+"\\temp\\";//获取项目的绝对路径
		
		// SVG临时文件名
		String temp = WebRoot+System.currentTimeMillis()+(int)(Math.random()*1000)+".svg";

		// 将svg代码写入到临时文件中，文件后缀的.svg
		File svgTempFile = new File(temp);
		//写入文件，注意文件编码
		OutputStreamWriter svgFileOsw = new OutputStreamWriter(new FileOutputStream(svgTempFile),"UTF-8");
		svgFileOsw.write(svg);
		svgFileOsw.flush();
		svgFileOsw.close();
		
		/**
		 * 第三步：调用转换函数，生成目标文件
		 */
		
		try {
			File resultFile = null;
			// 如果是导出SVG，直接调用下载，不需要将svg转换成其他格式
			if(type.equals("image/svg+xml")) {
				resultFile = new File(temp);
				filename = "chart.svg";
			} else {
				MyConverter mc = new MyConverter();
				// 调用转换函数，返回目标文件名
				filename = mc.conver(temp,WebRoot, type, Float.parseFloat(width));
				// 读取目标文件流，转换调用下载
				resultFile = new File(WebRoot+filename);
			}
			FileInputStream resultFileFi = new FileInputStream(resultFile);
			long l = resultFile.length();
			int k = 0;
			byte abyte0[] = new byte[65000];
			
			/**
			 * 第四步：调用浏览器下载
			 */
			if(method.equals("download")) {
				// 调用下载
				response.setContentType("application/x-msdownload");
				response.setContentLength((int) l);
				response.setCharacterEncoding("utf-8");
				response.setHeader("Content-Disposition", "attachment; filename=" + filename);
				while ((long) k < l) {
					int j;
					j = resultFileFi.read(abyte0, 0, 65000);
					k += j;
					response.getOutputStream().write(abyte0, 0, j);
				}
				resultFileFi.close();
				resultFile.delete();
			} else {
				
			}
			// 转换成功后，删除临时文件
			svgTempFile.delete();
			
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SVGConverterException e) {
			e.printStackTrace();
		}
		
		
	}

}
